package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDataPageReqDto;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.task.Content;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 设备数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-20
 */
@Service
@Slf4j
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService
{
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Autowired
    private DeviceMapper deviceMapper;


    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    @Override
    public DeviceData selectDeviceDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询设备数据列表
     * 
     * @param deviceData 设备数据
     * @return 设备数据
     */
    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
    {
        return deviceDataMapper.selectDeviceDataList(deviceData);
    }

    /**
     * 新增设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData)
    {
        return save(deviceData) ? 1 : 0;
    }

    /**
     * 修改设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData)
    {
        return updateById(deviceData) ? 1 : 0;
    }

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除设备数据信息
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 批量新增设备数据
     * @param content
     */
    @Override
    public void batchInsertDeviceData(Content content) {
        //1. 通过设备id查询设备
        Device device = deviceMapper.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getIotId, content.getIotId()));
        if (ObjectUtil.isNull(device)) {
            log.info("设备信息不存在:{}",content.getIotId());
            return;
        }
        //2. 构建设备数据
        List<DeviceData> dataList = new ArrayList<>();
        //循环构建
        content.getItems().forEach((k,v)->{
            DeviceData data = BeanUtil.toBean(device, DeviceData.class);
            //设置主键自增id
            data.setId(null);
            data.setFunctionId(k);
            data.setDataValue(String.valueOf(v.getValue()));
            data.setAlarmTime(LocalDateTimeUtil.of(v.getTime()));
            data.setAccessLocation(device.getRemark());
            dataList.add(data);
        });

        //3. 批量保存到数据库
        saveBatch(dataList);
    }

    /**
     * 查询设备数据列表
     * @param dto
     * @return
     */
    @Override
    public TableDataInfo<List<DeviceData>> queryPage(DeviceDataPageReqDto dto) {
        //构建分页
        Page page = Page.of(dto.getPageNum(), dto.getPageSize());
        //构建查询条件
        lambdaQuery().like(StrUtil.isNotEmpty(dto.getDeviceName()),DeviceData::getDeviceName,dto.getDeviceName())
                .ge(ObjectUtil.isNotEmpty(dto.getStartTime()),DeviceData::getAlarmTime,dto.getStartTime())
                .le(ObjectUtil.isNotEmpty(dto.getEndTime()),DeviceData::getAlarmTime,dto.getEndTime())
                .eq(StrUtil.isNotEmpty(dto.getFunctionId()),DeviceData::getFunctionId,dto.getFunctionId())
                .page(page);
        //构建返回数据
        TableDataInfo<List<DeviceData>> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setCode(0);
        return tableDataInfo;
    }
}
